package com.zzptc.fragmenttest1.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.zzptc.fragmenttest1.Bean.User;
import com.zzptc.fragmenttest1.R;

import org.litepal.LitePal;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class FirstFragment extends Fragment {
    private ListView lv;
    List<User>userList=new ArrayList<>();
    List<String>dataLite=new ArrayList<>();



    public FirstFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view=inflater.inflate(R.layout.fragment_first,container,false);
        initView(view);
        setListener();
        return view;
    }
    private void setListener(){
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showToast("你点击了"+dataLite.get(position));

            }
        });

    }


    private void initView(View view) {
        userList=LitePal.findAll(User.class);
        dataLite.clear();
        for(User u:userList) {
            dataLite.add("id:"+u.getId()+"   名字："+u.getName()+"   密码"+u.getPassword());

        }
        lv=view.findViewById(R.id.lv);

       lv=view.findViewById(R.id.lv);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_list_item_1,dataLite );
        lv.setAdapter(adapter);
    }
    private void showToast(String str){
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();

    }
}


